package 考试;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class 题4 {
    /**
     * 说明：从
     * score.txt 文件中读取数据放置到 List<Map> 中，文件中每行数据是一个 Map ，文件中数据
     * 为：姓名、班级、语文成绩、数学成绩、英语成绩、化学成绩，并按总分对 List 中数据排序，从高
     * 到低打印出每个学生的各科成绩和总成绩。 数据 文件见： score.txt
     */
    public static void main(String[] args) throws IOException {

        ArrayList<HashMap<String,String>> HashMapArrayList = new ArrayList<HashMap<String,String>>();
        File file = new File("C:\\Users\\User\\Desktop\\score(1).txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"utf-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            // 如果 t x t文件里的路径 不包含---字符串       这里是对里面的内容进行一个筛选
            HashMap<String,String> map = new HashMap<String,String>();
            StringBuffer stringBuffer = new StringBuffer("");
            Integer number = 0;
            int bz = 0;
            String name = "";
            for(int i=0;i<line.length();i++){
                name = name +line.substring(i,i+1);
                if(line.substring(i,i+1).equals(",")){
                    if (bz==0) map.put("姓名",name.substring(0,name.length()-1));
                    for(int ii=i+1;ii<line.length();ii++){
                        if(line.substring(ii,ii+1).equals(",")||ii==line.length()-1){
                            if (bz==0)map.put("班级",stringBuffer.toString());
                            if (bz==1)map.put("语文成绩",stringBuffer.toString());
                            if (bz==2)map.put("数学成绩",stringBuffer.toString());
                            if (bz==3)map.put("英语成绩",stringBuffer.toString());
                            if (ii==line.length()-1)
                            {
                                stringBuffer.append(line.substring(ii,ii+1));
                            }
                            if (bz==4)map.put("化学成绩",stringBuffer.toString());
                            if(bz!=0)number=number+Integer.parseInt(stringBuffer.toString());
                            stringBuffer=new StringBuffer("");
                            bz++;
                            break;
                        }
                        stringBuffer.append(line.substring(ii,ii+1));
                    }
                }
            }
            map.put("总成绩",String.valueOf(number));
            HashMapArrayList.add(map);
        }

        //排序
        HashMapArrayList.sort((HashMap<String,String> a,HashMap<String,String> b)->{
            return Integer.parseInt(b.get("总成绩"))-Integer.parseInt(a.get("总成绩"));
        });


        //输出

        for (HashMap<String,String> hash:HashMapArrayList) {
            System.out.println("姓名："+hash.get("姓名")+
                    " 班级"+hash.get("班级")+
                    " 语文成绩"+hash.get("语文成绩")+
                    " 数学成绩"+hash.get("数学成绩")+
                    " 英语成绩"+hash.get("英语成绩")+
                    " 化学成绩"+hash.get("化学成绩")+
                    " 总成绩"+hash.get("总成绩")
                    );
        }

    }
}
